package com.joelbitar.switchctrl;

import java.util.ArrayList;
import java.util.jar.Attributes.Name;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.ImageView;

public class DevicesListAdapter extends ArrayAdapter<Device> {
	private ArrayList<Device> devices;
	private Context context;
	
	public DevicesListAdapter(Context context, int textViewResourceId, ArrayList<Device> devices) {
		super(context, textViewResourceId, devices);
		
		this.context = context;
		this.devices = devices;
	}
	
	public void setDevices(ArrayList<Device> devices)
	{
		this.devices = devices;
		this.notifyChange();
	}
	
	
	
	public void notifyChange()
	{
		this.notifyDataSetChanged();
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.device_switch, null);
        }
        
        Device device = this.devices.get(position);
        
        // Set the tag to be the device.
        v.setTag(device);
        
        TextView name = (TextView) v.findViewById(R.id.name);
        name.setText(device.getName());
        
        TextView server = (TextView) v.findViewById(R.id.server);
        server.setText(device.getServer().getName());
        
        ImageView status = (ImageView) v.findViewById(R.id.status);
        
        if(device.view_icon_resource != device.getIconResource())
        	status.setImageResource(device.getIconResource());
        
        
        // TODO if other type than default.. blah.
        
        ImageView markedOn 	= (ImageView) v.findViewById(R.id.on_button);
        ImageView markedOff = (ImageView) v.findViewById(R.id.off_button);
        markedOn.setImageResource(R.drawable.switch_on_inactive);
        markedOff.setImageResource(R.drawable.switch_off_inactive);
        
        if(device.getMarked() == 1)
        {
        	if(device.view_marked_on != R.drawable.switch_on_active)
        		markedOn.setImageResource(R.drawable.switch_on_active);
        }
        else if(device.getMarked() == 0)
        {
        	if(device.view_marked_off != R.drawable.switch_off_active)
        		markedOff.setImageResource(R.drawable.switch_off_active);
        }
        
        TextView requestStatus = (TextView) v.findViewById(R.id.request_status_text);
        
        if(device.getRequestInProgress())
        {
        	Context context = getContext();
        	Resources res = context.getResources();
        	
        	// the request string, trimmed and appended.
        	// First gotten with getstring
        	// then formated and the name replaced with ""
        	// then trimmed
        	// then appended with (resumably) ...
        	// The result (something similar to) "turning off..."
        	String requestStatusString = String.format(
        			context.getString(device.getRequestInProgressTextResource()) , ""
        	).trim() + context.getString(R.string.device_request_status_append);
        	
        	requestStatusString = String.format(requestStatusString, "");
        	
        	requestStatus.setText(requestStatusString);
        	
        	int requestStatusColor;
        	// Setting the color
        	if(device.getRequestStatus() == 0)
        	{
        		requestStatusColor = R.color.device_status_color_sending_off;
        	}
        	else
        	{
        		requestStatusColor = R.color.device_status_color_sending_on;
        	}
        	
        	// Setting the textcolor
    		requestStatus.setTextColor(res.getColor(requestStatusColor));
        	
        	
        }
        else
        {
        	requestStatus.setText(R.string.device_request_status_none);
        }
        
        //Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.loading_rotate);
        
        //anim.setInterpolator(getContext(), R.anim.linear_interpolation);

    	
        
        /*
        
        FrameLayout loading_holder = (FrameLayout) v.findViewById(R.id.loading_holder);
        
        loading_holder.setVisibility(0);
        
        ImageView loading = (ImageView) v.findViewById(R.id.loading_anim);
        
        //Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.pulsate_slow);
        
        // If status is not known (Thats if its unknown for several reasons)
        // unknown	= show the loading-animation
        // known	= hide the loading animation
        int loading_animation_id = (device.getStatus() == Device.STATUS_NOT_KNOWN) ? R.anim.device_loading_fadein : R.anim.device_loading_fadeout;
        
        loading_animation_id = R.anim.device_loading_fadein;
        //loading_animation_id = R.anim.device_loading_fadein;
        
        Animation loading_animation = AnimationUtils.loadAnimation(getContext(), loading_animation_id);
        
        //loading.startAnimation(rotate_anim);
        loading.startAnimation(loading_animation);
        
        /* */
        
        v.setTag(device);
        return v;
	}
	/*
	public void startAnim(View v)
	{
		Device device = (Device) v.getTag();

        if(device.view_loading_animation_running == false)
        {
        	ImageView loading = (ImageView) v.findViewById(R.id.loading_anim);
        	device.view_loading_animation_running = true;
        	
            Animation loading_animation = AnimationUtils.loadAnimation(getContext(), R.anim.loading_rotate);
            
            loading_animation.setInterpolator(getContext(), R.anim.linear_interpolation);
            loading.startAnimation(loading_animation);
        }
		
	}
	*/
	
	// Set new list of devices
	public void updateDevices(ArrayList<Device> devices) {
		this.notifyDataSetChanged();
		this.devices.clear();
		for(int i = 0; i < devices.size(); i += 1)
		{
			this.devices.add(devices.get(i));
		}
		this.notifyDataSetChanged();
	}
	
	// Clear all marked devices
	public void clearMarked() {
		this.clearMarkedDevices(this.devices);
	}
	private int clearMarkedDevices(ArrayList<Device> devices){
		int deviceCount = 1;
		
		for(int i = 0; i < devices.size(); i += 1)
		{
			Device device = devices.get(i);
			device.setMarked(Device.SWITCH_MARKED_NONE);
			
			deviceCount += 1;
		}
		return deviceCount;
	}
	
	
}
